#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QString>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlError>
#include <QTreeWidgetItem>
#include <QTreeWidgetItemIterator>
#include <QList>
#include <QResizeEvent>
#include <QKeyEvent>
#include "newarticle.h"
#include "newnode.h"

namespace Ui {
class MainWindow;
}

typedef struct _ARTICLE_RETURN_ {
    QList<Article> article;
    int combo_index;
} ArticleReturn;

typedef struct _ARTICLE_INFO_ {
    int id;
    bool is_node;
    QString title;
    QString content;
} ArticleInfo;

class MainWindow : public QMainWindow
{
    Q_OBJECT

    QSqlQuery* q_exec(QString sql, bool is_exit = true);

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private slots:
    void receive_article(QString title);
    void receive_node(int article_id, int parent_node_id, QString title, QString content);
    void on_new_article_triggered();

    void on_new_node_triggered();

    void on_article_tree_widget_itemClicked(QTreeWidgetItem *item, int);

    void on_delete_node_triggered();

    void on_title_line_edit_textEdited(const QString &arg1);

    void on_flush_triggered();

    void on_content_text_edit_textChanged();

protected:
    void resizeEvent(QResizeEvent *event);
    void keyPressEvent(QKeyEvent *event);

private:
    Ui::MainWindow *ui;
    NewArticle *new_article;
    NewNode *new_node;
    QSqlDatabase db;
    QSqlError err;
    QList<Article> article_list;
    int current_id;
    ArticleInfo info;
    void next_tree(QTreeWidgetItem *item, int article_id, int parent_node_id);
    void flush_tree();
    ArticleReturn next_article(ArticleReturn ret, int article_id, int node_id);
    ArticleInfo sub_node_info(int line, QList<Article> list);
    ArticleInfo node_info(int line);
    void reset_article_list();
    void delete_sub_node(int parent_id);
};

#endif // MAINWINDOW_H
